<template>
  <view class="booking-stats-container">
    <view class="header">
      <text class="title">预约统计</text>
    </view>
    
    <!-- 时间筛选 -->
    <view class="filter-section">
      <picker mode="date" :value="startDate" @change="onStartDateChange">
        <view class="picker">
          {{ startDate }}
        </view>
      </picker>
      <text class="divider">至</text>
      <picker mode="date" :value="endDate" @change="onEndDateChange">
        <view class="picker">
          {{ endDate }}
        </view>
      </picker>
      <button class="filter-btn" @click="fetchBookingStats">查询</button>
    </view>
    
    <!-- 预约统计概览 -->
    <view class="overview-section">
      <view class="stat-card">
        <text class="stat-value">{{ bookingStats.totalBookings }}</text>
        <text class="stat-label">总预约数</text>
      </view>
      
      <view class="stat-card">
        <text class="stat-value">{{ bookingStats.confirmedBookings }}</text>
        <text class="stat-label">已确认</text>
      </view>
      
      <view class="stat-card">
        <text class="stat-value">{{ bookingStats.completedBookings }}</text>
        <text class="stat-label">已完成</text>
      </view>
      
      <view class="stat-card">
        <text class="stat-value">{{ bookingStats.conversionRate }}%</text>
        <text class="stat-label">转化率</text>
      </view>
    </view>
    
    <!-- 预约趋势图表 -->
    <view class="trend-section">
      <text class="section-title">预约趋势</text>
      <view class="chart-placeholder">
        <text>预约趋势图表</text>
      </view>
    </view>
    
    <!-- 课程预约排行 -->
    <view class="ranking-section">
      <text class="section-title">课程预约排行</text>
      <view class="ranking-list">
        <view class="ranking-item" v-for="(course, index) in bookingStats.courseRankings" :key="course.id">
          <view class="rank-number" :class="{ 'top-three': index < 3 }">
            {{ index + 1 }}
          </view>
          <view class="course-info">
            <text class="course-name">{{ course.name }}</text>
            <text class="course-bookings">{{ course.bookings }}人预约</text>
          </view>
          <view class="booking-rate">
            <text>{{ course.rate }}%</text>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 教练预约排行 -->
    <view class="ranking-section">
      <text class="section-title">教练预约排行</text>
      <view class="ranking-list">
        <view class="ranking-item" v-for="(coach, index) in bookingStats.coachRankings" :key="coach.id">
          <view class="rank-number" :class="{ 'top-three': index < 3 }">
            {{ index + 1 }}
          </view>
          <view class="coach-info">
            <text class="coach-name">{{ coach.name }}</text>
            <text class="coach-bookings">{{ coach.bookings }}人预约</text>
          </view>
          <view class="booking-rate">
            <text>{{ coach.rate }}%</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { statisticsApi } from '../../../src/services/statistics'

// 日期筛选
const startDate = ref('')
const endDate = ref('')

// 预约统计数据
const bookingStats = ref({
  totalBookings: 0,
  confirmedBookings: 0,
  completedBookings: 0,
  conversionRate: 0,
  courseRankings: [] as any[],
  coachRankings: [] as any[]
})

// 初始化日期
const initDates = () => {
  const now = new Date()
  const oneMonthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate())
  
  startDate.value = formatDate(oneMonthAgo)
  endDate.value = formatDate(now)
}

// 格式化日期
const formatDate = (date: Date) => {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 开始日期变更
const onStartDateChange = (e: any) => {
  startDate.value = e.detail.value
}

// 结束日期变更
const onEndDateChange = (e: any) => {
  endDate.value = e.detail.value
}

// 获取预约统计数据
const fetchBookingStats = async () => {
  try {
    const params = {
      startDate: startDate.value,
      endDate: endDate.value
    }
    const response: any = await statisticsApi.getBookingStats(params)
    if (response.code === 200) {
      bookingStats.value = response.data
    }
  } catch (error) {
    console.error('获取预约统计数据失败:', error)
    uni.showToast({
      title: '获取数据失败',
      icon: 'error'
    })
  }
}

onMounted(() => {
  initDates()
  fetchBookingStats()
})
</script>

<style scoped>
.booking-stats-container {
  background-color: #ffffff;
  min-height: 100vh;
  padding: 20rpx;
}

.header {
  text-align: center;
  margin-bottom: 40rpx;
  padding-top: 20rpx;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333333;
}

.filter-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #f8f8f8;
  border-radius: 16rpx;
  padding: 20rpx;
  margin-bottom: 40rpx;
}

.picker {
  background-color: #ffffff;
  border-radius: 8rpx;
  padding: 10rpx 20rpx;
  font-size: 24rpx;
}

.divider {
  margin: 0 20rpx;
  color: #666666;
}

.filter-btn {
  background-color: #4B0082;
  color: white;
  border: none;
  border-radius: 8rpx;
  padding: 10rpx 20rpx;
  font-size: 24rpx;
}

.overview-section {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
  margin-bottom: 40rpx;
}

.stat-card {
  background-color: #f8f8f8;
  border-radius: 16rpx;
  padding: 30rpx 20rpx;
  text-align: center;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.stat-value {
  display: block;
  font-size: 36rpx;
  font-weight: bold;
  color: #4B0082;
  margin-bottom: 8rpx;
}

.stat-label {
  font-size: 24rpx;
  color: #666666;
}

.section-title {
  display: block;
  font-size: 32rpx;
  font-weight: bold;
  color: #333333;
  margin-bottom: 20rpx;
}

.trend-section {
  background-color: #f8f8f8;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 40rpx;
}

.chart-placeholder {
  height: 300rpx;
  background-color: #eeeeee;
  border-radius: 12rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart-placeholder text {
  color: #999999;
  font-size: 28rpx;
}

.ranking-list {
  background-color: #f8f8f8;
  border-radius: 16rpx;
  padding: 20rpx;
  margin-bottom: 40rpx;
}

.ranking-item {
  display: flex;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #eeeeee;
}

.ranking-item:last-child {
  border-bottom: none;
}

.rank-number {
  width: 50rpx;
  height: 50rpx;
  border-radius: 50%;
  background-color: #dddddd;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  margin-right: 20rpx;
}

.rank-number.top-three {
  background-color: #4B0082;
  color: white;
}

.course-info, .coach-info {
  flex: 1;
}

.course-name, .coach-name {
  display: block;
  font-size: 28rpx;
  color: #333333;
  margin-bottom: 8rpx;
}

.course-bookings, .coach-bookings {
  font-size: 20rpx;
  color: #999999;
}

.booking-rate {
  font-size: 28rpx;
  font-weight: bold;
  color: #4B0082;
}
</style>